package com.controller;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mypg.*;
import com.service.CommentService;
import com.service.QuestionService;
import com.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RequestMapping("/comment")
@Controller
public class CommentController {

    @Autowired
    CommentService commentService;
    @Autowired
    UserService userService;
    @Autowired
    QuestionService questionService;

    @RequestMapping("/comment_table_select.do")
    @ResponseBody
    public String doComment_table_select(String user_nickname, String question_id,String time_start, String time_end,
                                      int page, int limit) throws ParseException, JsonProcessingException {
        int start_row=(page-1)*limit;
        int page_nums=limit;
        int question_id_int=0;
        if(question_id==null||question_id=="")
            question_id_int=0;
        else{
            question_id_int=Integer.parseInt(question_id);
        }
        Timestamp timestamp_start;
        Timestamp timestamp_end;
        SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm");
        if(time_start==null||time_start==""){
            time_start="1970-01-01 01:01";
            timestamp_start=new Timestamp(df.parse(time_start).getTime());
        }else{
            time_start=time_start.replace("T"," ");
            timestamp_start=new Timestamp(df.parse(time_start).getTime());
        }

        if(time_end==null||time_end==""){
            timestamp_end=new Timestamp(System.currentTimeMillis());
        }else{
            time_end=time_end.replace("T"," ");
            timestamp_end=new Timestamp(df.parse(time_end).getTime());
        }
        HashMap<String,Object> map=new HashMap<>();
        map.put("code",0);
        map.put("msg","");
        map.put("count",commentService.selectComment_count_For_comment_table_jsp(user_nickname,question_id_int,timestamp_start,timestamp_end));
        List<Comment> commentList=commentService.selectComment_for_comment_table_jsp(user_nickname,question_id_int,timestamp_start,timestamp_end,start_row,page_nums);
        map.put("data", CommentHelper.changeCommentListToCommentHelperList(commentList));
        ObjectMapper mapper=new ObjectMapper();
        String map_json_str=mapper.writeValueAsString(map);
        return map_json_str;
    }

    @RequestMapping("/comment_table_delete_one.do")
    @ResponseBody
    public void doNews_table_delete_one(int comment_id){
        commentService.deleteCommentByComment_id(comment_id);
    }

    @RequestMapping("/comment_table_delete_many.do")
    @ResponseBody
    public int doComment_table_delete_many(String[] comment_id_array){
        comment_id_array[0]=comment_id_array[0].replace("[","");
        comment_id_array[comment_id_array.length-1]=comment_id_array[comment_id_array.length-1].replace("]","");
        for(int i=0;i<comment_id_array.length;i++){
            commentService.deleteCommentByComment_id(Integer.parseInt(comment_id_array[i]));
        }
        return 0;
    }

    @RequestMapping("/comment_table_user_info_init.do")
    @ResponseBody
    public User doComment_table_user_info_init(int user_id){
        return userService.selectUserByUser_id(user_id);
    }

    @RequestMapping("/comment_table_question_info_init.do")
    @ResponseBody
    public Question doComment_table_question_info_init(int question_id){
        return questionService.selectQuestionByQuestion_id(question_id);
    }

}
